package com.zhanghang.vending_machine.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhanghang.vending_machine.domain.po.Good;
import com.zhanghang.vending_machine.domain.po.ShoppingCart;
import com.zhanghang.vending_machine.mapper.GoodMapper;
import com.zhanghang.vending_machine.service.ShoppingCartService;
import com.zhanghang.vending_machine.mapper.ShoppingCartMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author ASUS
* @description 针对表【shopping_cart】的数据库操作Service实现
* @createDate 2024-11-06 14:06:29
*/
@Service
@RequiredArgsConstructor
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>
    implements ShoppingCartService{

    private final GoodMapper goodMapper;

    private final ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加购物车
     * @param goodId
     * @param number
     */
    @Override
    @Transactional
    public void addShoppingCart(Long goodId, Integer number) {
        //查询商品信息
        Good good = goodMapper.selectById(goodId);

        //若商品为空抛出异常
        if (good == null){
            throw new RuntimeException("商品信息异常");
        }

        //查询购物车，如果购物车没有该商品信息，则创建该条数据
        ShoppingCart shoppingCart = lambdaQuery()
                .eq(ShoppingCart::getGoodId, goodId)
                .one();
        shoppingCart = shoppingCart;

        if (shoppingCart == null) {
            //插入一条数据
            ShoppingCart shoppingCart1 = new ShoppingCart();
            //将good中的属性拷贝
            BeanUtils.copyProperties(good,shoppingCart1);
            //设置数量信息
            shoppingCart1.setNumber(number);
            //设置商品id
            shoppingCart1.setGoodId(goodId);
            //设置金额
            shoppingCart1.setAmount(number * good.getPrice());
            //设置创建时间
            shoppingCart1.setCreateTime(LocalDateTime.now());

            //添加进购物车
            save(shoppingCart1);
        }else {
            //已经存在数据只需修改，number和amount
            shoppingCart.setNumber(shoppingCart.getNumber() + number);
            shoppingCart.setAmount(number * good.getPrice());

            //修改表信息
            lambdaUpdate()
                    .set(ShoppingCart::getNumber,shoppingCart.getNumber())
                    .set(ShoppingCart::getAmount,shoppingCart.getAmount())
                    .eq(ShoppingCart::getGoodId,goodId)
                    .update();
        }
    }

    /**
     * 删除购物车内商品
     * @param goodId
     */
    @Override
    public void subByGoodId(Long goodId) {
        //查询购物车记录数据
        ShoppingCart shoppingCart = lambdaQuery()
                .eq(ShoppingCart::getGoodId, goodId)
                .one();

        if (shoppingCart == null){
            throw new RuntimeException("参数信息有误");
        }

        //修改number属性
        Integer newNumber = shoppingCart.getNumber() - 1;

        //如果减一后newNumber为0，则删除整条数据
        if (newNumber == 0){
            removeById(shoppingCart.getId());
        }else {
            //修改number信息
            lambdaUpdate()
                    .set(ShoppingCart::getNumber,newNumber)
                    .eq(ShoppingCart::getGoodId,goodId)
                    .update();
        }
    }

    /**
     * 清空购物车
     */
    @Override
    public void sub() {
        List<Long> ids = shoppingCartMapper.getIds();

        removeBatchByIds(ids);
    }
}




